﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using QXGameEngine.Interface;
using QXGameEngine.Logic;

namespace QXGameEngine.Control {

    public partial class QXTeleport : UserControl ,IObject {

        /// <summary>
        /// 传送点控件
        /// </summary>
        public QXTeleport() {
            InitializeComponent();
            Timer = new DispatcherTimer();
            Timer.Interval = TimeSpan.FromMilliseconds(150);
            Timer.Tick += new EventHandler(Timer_Tick);
            Timer.Start();
        }

        /// <summary>
        /// 获取或设置内置计时器
        /// </summary>
        public DispatcherTimer Timer { get; set; }

        /// <summary>
        /// 获取或设置根画布容器
        /// </summary>
        public Canvas RootCanvas {
            get { return this.Container; }
        }

        /// <summary>
        /// 获取父容器
        /// </summary>
        public Canvas ParentCanvas {
            get { return this.Parent as Canvas; }
        }

        /// <summary>
        /// 获取或设置提示文本
        /// </summary>
        public string Tip {
            get { return Details.Text; }
            set { Details.Text = value; }
        }

        /// <summary>
        /// 获取或设置代号
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 获取或设置传送点所处于障碍物数组中的标识
        /// </summary>
        public byte Key { get; set; }

        /// <summary>
        /// 获取或设置传送到的地图
        /// </summary>
        public int To { get; set; }
        
        /// <summary>
        /// 获取或设置传送到新地图后主角出现的X值
        /// </summary>
        public double ToX { get; set; }

        /// <summary>
        /// 获取或设置传送到新地图后主角出现的Y值
        /// </summary>
        public double ToY { get; set; }

        /// <summary>
        /// 获取或设置传送到新地图后主角出现的朝向
        /// </summary>
        public double ToDirection { get; set; }

        /// <summary>
        /// 获取或设置帧推进器
        /// </summary>
        public int FrameCounter { get; set; }

        /// <summary>
        /// 获取或设置物体当前开始图片列号
        /// </summary>
        public int StartFrame { get; set; }

        /// <summary>
        /// 获取或设置物体当前结束图片列号
        /// </summary>
        public int EndFrame { get; set; }

        /// <summary>
        /// 获取或设置控件主体宽
        /// </summary>
        public double BodyWidth {
            get { return Container.Width; }
            set { Container.Width = value; }
        }

        /// <summary>
        /// 获取或设置控件主体高
        /// </summary>
        public double BodyHeight {
            get { return Container.Height; }
            set { Container.Height = value; }
        }

        /// <summary>
        /// 获取或设置物体中心点距离左边X坐标
        /// </summary>
        public double CenterX { get; set; }

        /// <summary>
        /// 获取或设置物体中心点距离顶边Y坐标
        /// </summary>
        public double CenterY { get; set; }

        /// <summary>
        /// 获取或设置坐标(关联属性)
        /// </summary>
        public Point Coordinate {
            get { return (Point)GetValue(CoordinateProperty); }
            set { SetValue(CoordinateProperty, value); }
        }
        public static readonly DependencyProperty CoordinateProperty = DependencyProperty.Register(
            "Coordinate",
            typeof(Point),
            typeof(QXTeleport),
            new PropertyMetadata(ChangeCoordinateProperty)
        );
        private static void ChangeCoordinateProperty(DependencyObject d, DependencyPropertyChangedEventArgs e) {
            QXTeleport obj = (QXTeleport)d;
            if (obj.Visibility == Visibility.Visible) {
                Point p = (Point)e.NewValue;
                obj.SetValue(Canvas.LeftProperty, p.X - obj.CenterX);
                obj.SetValue(Canvas.TopProperty, p.Y - obj.CenterY);
                obj.SetValue(Canvas.ZIndexProperty, (int)p.Y);
            }
        }

        /// <summary>
        /// 计时器事件
        /// </summary>
        private void Timer_Tick(object sender, EventArgs e) {
            Container.Background = new ImageBrush() { ImageSource = Super.GetImage(string.Format(@"/Image/Item/{0}/{1}.png", Code, FrameCounter)) };
            FrameCounter = FrameCounter == EndFrame ? StartFrame : FrameCounter + 1;
        }
    }
}
